<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import = "whf.framework.lucene.searcher,javax.servlet.*,javax.servlet.http.*,java.io.*,java.text.DecimalFormat,org.apache.lucene.analysis.*,org.apache.lucene.analysis.standard.StandardAnalyzer,org.apache.lucene.document.*,org.apache.lucene.index.*,org.apache.lucene.search.*,org.apache.lucene.queryParser.*,org.apache.lucene.demo.*,org.apache.lucene.demo.html.Entities"%>
<%
/*
        Author: Andrew C. Oliver, SuperLink Software, Inc. (acoliver2@users.sourceforge.net)

        This jsp page is deliberatly written in the horrble java directly embedded 
        in the page style for an easy and conceise demonstration of Lucene.
        Due note...if you write pages that look like this...sooner or later
        you'll have a maintenance nightmere.  If you use jsps...use taglibs
        and beans!  That being said, this should be acceptable for a small
        page demonstrating how one uses Lucene in a web app. 

        This is also deliberately overcommented. ;-)

*/
%> 
<html>
<%@include file="header.jsp"%>
<body  vlink="#6600FF" alink="#33CCFF">

<%
        boolean error = false;                  //used to control flow for error messages
   
        Searcher searcher = null;          //the searcher used to open/search the index
        
        Query queryField1 = null;                     //the Query created by the QueryParser
        Query queryField2 = null;
        Query queryField3 = null;
        BooleanQuery bQuery = null;
        Hits hits = null;                       //the search results
        int startindex = 0;                     //the first index displayed on this page
        int maxpage    = 10;                    //the maximum items displayed on this page
        String queryString = null;              //the query entered in the previous page
        String startVal    = null;              //string version of startindex
        String maxresults  = null;              //string version of maxpage
        int thispage = 0;                       //used for the for/next either maxpage or
                                                //hits.length() - startindex - whichever is
                                                //less
		boolean checkRLabel=false;
		boolean checkWLabel=false;
		boolean checkALabel=false;
		

		String label=request.getParameter("label");
		String checkR=request.getParameter("checkboxR"); 
		if(checkR!=null){
		if (checkR.equals("1"))
			indexLocation=locatR;
		if (checkR.equals("2"))
			indexLocation=locatW;
		if (checkR.equals("3"))
			indexLocation=locatA;
		}else{
			indexLocation=locatR;//default locaded to people
			checkR="1";
		}
		checkR="1";
     	String indexName = indexLocation;       //local copy of the configuration variable
		String userPCSName = "";//褰撳墠鐢ㄦ埛鎵�灞炴淳鍑烘墍鍚嶇О
		if(session.getAttribute("userPCSName")!= null){
			userPCSName = (String)session.getAttribute("userPCSName");
		}
%>

<%
//鏌ヨ绫诲埆锛屼汉鍛橈紵鍗曚綅锛熸埧灞�
String searchObjectType =request.getParameter("c");
searchObjectType = searchObjectType== null?"":searchObjectType;

queryString = request.getParameter("query"); 
//add by wanghaifeng, using base64 decode
boolean firstSearch = "true".equals(request.getParameter("firstSearch"));
if(queryString != null){
	if(!firstSearch){
		queryString = (String)session.getAttribute("current_query");//new String(com.hnisi.util.Base64.decode(queryString));
	} else {
		session.setAttribute("current_query", queryString);
	}
}
//System.out.println("queryString="+queryString);
%>
<script language="javascript">
function searchNow(){
	if(form1.query.value == ""){
		alert("璇疯緭鍏ユ悳绱㈡潯浠讹紒");
		return;
	}else{
		form1.submit();
	}
}
</script>
<table cellspacing=0 cellpadding=0>
<form name="form1" method="post" action="results.jsp">
<tr>
	<td rowspan="2" ><img src="../images/soubar.gif" width="150"></td>
	<td><font size=-1>璇疯緭鍏ユ悳绱㈡潯浠讹細</font></td>
	<td><input type="text" name="query" size="30" value="<%= queryString == null?"":queryString %>"></td>
	<td><input type="button" value="鎼� 鍚�" onclick="searchNow()"></td>
</tr>
<tr>
	<td></td>
	<td colspan=2>
	<input type="radio" name="c" value="1" <% if("1".equals(searchObjectType)) { %>checked<% } %>><font size=-1>浜哄憳淇℃伅</font>
	<input type="radio" name="c" value="2" <% if("2".equals(searchObjectType)) { %>checked<% } %>><font size=-1>鎴垮眿淇℃伅</font>
	<input type="radio" name="c" value="3" <% if("3".equals(searchObjectType)) { %>checked<% } %>><font size=-1>浼佷笟淇℃伅</font>
	<input type="radio" name="c" value="" <% if("".equals(searchObjectType)) { %>checked<% } %>><font size=-1>鎵�鏈変俊鎭�</font>
	</td>
</tr>
<input type="hidden" name="firstSearch" value="true">
</form>
</table>
<% if(queryString != null) {%>
<%
        long spendTime = 0;
        org.apache.lucene.demo.HightLighter hl = null;
		org.apache.lucene.demo.HightLighter [] hArry = null;

        try {
 //       searcher = new IndexSearcher(IndexReader.open(indexName));  //create an indexSearcher for our page
 			searcher = SearcherManager.getSearcher();
        } catch (Exception e) {//any error that happens is probably due to a permission problem or non-existant or otherwise corrupt index
        	//System.out.println("閿欒鎵撳紑绱㈠紩鏂囦欢 - 璇疯仈绯荤鐞嗗憳!");
	        e.printStackTrace();
            error = true; 
        }
%>

<%
	//end
       if (error == false&queryString!=null) {                                 //did we open the index?
                        //  get the search criteria
						//  if (label==null)
                //queryString = new String(queryString.getBytes("iso-8859-1") , "gb2312");
						 // //System.out.println("queryString="+queryString);
			
          startVal    = request.getParameter("startat");         //get the start index
          maxresults  = request.getParameter("maxresults");      //get max results per page
          try {
              maxpage    = Integer.parseInt(maxresults);    //parse the max results first
              startindex = Integer.parseInt(startVal);      //then the start index  
          } catch (Exception e) { } //we don't care if something happens we'll just start at 0
                                    //or end at 50
          //org.apache.lucene.analysis.cn.ChineseAnalyzer   analyzer=  new org.apache.lucene.analysis.cn.ChineseAnalyzer();          //construct our usual analyzer
          //org.apache.lucene.analysis.cjk.CJKAnalyzer analyzer=new org.apache.lucene.analysis.cjk.CJKAnalyzer();
		  StandardAnalyzer analyzer = new StandardAnalyzer(); 
		  //SimpleAnalyzer analyzer = new SimpleAnalyzer();

          try {
	            if (queryString!=null) queryString=queryString.toString();
				queryField1 = QueryParser.parse( queryString , "content", analyzer);
				//寰呭姞鍏ユ淳鍑烘墍鐨勯檺鍒�
				bQuery = new BooleanQuery();
				if(!"".equals(searchObjectType)){
					queryField3 = QueryParser.parse( searchObjectType , "objectType", analyzer);
					bQuery.add(queryField3,true,false);
				}
				bQuery.add(queryField1,true,false);
				//if(!"".equals(userPCSName)){
				//	queryField2 = QueryParser.parse( userPCSName , "type", analyzer);
				//	bQuery.add(queryField2,true,false);
				//}

						
				//		query = QueryParser.parse( queryString , "content", analyzer); //parse the 
                //System.out.println("Searching for: " + bQuery.toString("content"));
                hl = new org.apache.lucene.demo.HightLighter(bQuery.toString("content"));
						//Hits hits = searcher.search(query);
                } catch (ParseException e) {                          //query and construct the Query object if its just "operator error" send them a nice error HTML
                                                                      
%>
                        <p>鏌ヨ鏉′欢褰曞叆鍑洪敊: 鍏ㄦ枃妫�绱笉鏀寔瀵硅繖浜涘瓧绗︿覆鐨勬绱紒</p>
<%
                        error = true;                                 //don't bother with the rest of
                                                                      //the page
                }
        }
%>
<%
        if (error == false && searcher != null) {                     // if we've had no errors
                                                                      // searcher != null was to handle
                                                                      // a weird compilation bug 
                thispage = maxpage;                                   // default last element to maxpage
                long beginTime = System.currentTimeMillis();
                hits = searcher.search(bQuery);                        // run the query 
                spendTime = System.currentTimeMillis() - beginTime;
                if(firstSearch){
                	LogService.addQueryLog(request,"鍏ㄦ枃妫�绱紝妫�绱㈡潯浠讹細" + queryString ,hits.length());
                }
                if (hits.length() == 0) {                             // if we got no results tell the user
%>
                <p><font size=-1> 鎶辨瓑,鎴戜滑娌℃湁鎵惧埌浣犻渶瑕佺殑鍐呭锛岃纭鏌ヨ鍐呭涓庢煡璇㈢被鍨嬫槸鍚﹀尮閰嶏紒 </font></p> 
<%
                error = true;                                        // don't bother with the rest of the
                                                                     // page
                }
        }
%>
<%
if (error == false && searcher != null) {
%>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <thead>
    <tr bgcolor=#e5ecf9>
    <td colspan="2" align="right">
    <font face="鏂板畫浣�" size="2">鎼滅储鏉′欢:<font color="red"><b><%=queryString%></b></font>&nbsp;&nbsp;鍏辨壘鍒�<font color="red"><b><%=hits.length()%></b></font>鏉¤褰�&nbsp;&nbsp;鏌ヨ鐢ㄦ椂:<font color="red"><b><%=spendTime%></b></font>寰</font>
    </td>
    </tr>
    </thead>
<%
	if ((startindex + maxpage) > hits.length()) {
		thispage = hits.length() - startindex;      // set the max index to maxpage or last
	}                                                   // actual search result whichever is less

	//System.out.println(startindex + maxpage+"闀垮害"+hits.length());
	for (int i = startindex; i < (thispage + startindex); i++) {  // for each element
%>
                
<%
        Document doc = hits.doc(i);                    //get the next document 
        String doctitle = doc.get("content");            //get its title
        String url = doc.get("key"); 
		String headStr=doc.get("head");
		if(headStr=="") headStr="鏌ョ湅璇︾粏淇℃伅";
		if(url!=null) url=url.trim();
		//get its url field
		//url = "/" + url.substring(5);
        String path = doc.get("content");
        
        doctitle = hl.getHighlight(doctitle);
       // url = url.toUpperCase();
        url = url.replaceAll("TABLENAME","tableid");
%>
						<tr height="30">
						<td><p>
                        <font face="鏂板畫浣�" size="-1"><p><a href="<%=request.getContextPath()%>/servlet/DetailInfo?<%=url%>"  target="_blank" ><%=headStr%></a></font>
                        </td>
                        <td width="1%" nowrap>
                        <%
                        if(url.toLowerCase().indexOf("rybh") != -1 || url.toLowerCase().indexOf("fhbh") != -1){
                        url = url.toUpperCase().replaceAll("&AMP;","&");
                        String[] ss = url.split("&");
                        java.util.HashMap map = new java.util.HashMap();
                        for(int j=0;j<ss.length;j++){
                        	String[] ss2 = ss[j].split("=");
                        	if(ss2!=null && ss2 .length == 2){
                        		map.put(ss2[0],ss2[1]);
                        	}
                        }
                        ////System.out.println(map);
                        ////System.out.println(url);
                        if(map.keySet().contains("FHBH")){
                        	String FHBH = (String)map.get("FHBH");
                        %>
						<a href="#" onclick="javascript:window.open('<%= request.getContextPath() %>/query/FWFH_lx.jsp?FHBH=<%= FHBH %>&hasOther=false&view=false&submitself=true',null,'left=300,top=200,width=400,height=340')"><font size="-1">杩涜鎴垮眿鍒嗙被</font></a>
						<% } else if(map.keySet().contains("RYBH")){ 
							String RYBH = (String)map.get("RYBH");
						%>
						<a href="#" onclick="javascript:window.open('<%= request.getContextPath() %>/query/RYFL_lx.jsp?RYBH=<%= RYBH %>&hasOther=false&view=false&submitself=true',null,'left=300,top=200,width=400,height=340')"><font size="-1">杩涜浜哄憳鍒嗙被</font></a>
						<% }
                        }%>
						</td>
						</tr>
						<tr height="15">
						<td colspan="2"><P>
                        <font face="鏂板畫浣�" size="-1"><%=doctitle%></font>
						</td>
						</tr>
			<%}%>
	</table>
	<br>
	<div align="center">
	<%                
	//add by wanghaifeng
	queryString = com.hnisi.util.Base64.encode(queryString);
	
	//end
	if ( startindex > 0 ) {
		String foreurl = "results.jsp?query=" + queryString + "&c="+searchObjectType+"&maxresults=" + maxpage + 
				"&startat=" + (startindex - maxpage)+ "&checkboxR="+checkR+"&label=1";
	%>
	<font face="鏂板畫浣�" size="2"><a href="<%=foreurl%>"><img src="../images/left.gif" border='0' width="15">鍓嶄竴椤�</a></font>
	<%
	}
	int totalPages = hits.length() / maxpage;
	int cpage = (startindex / maxpage) + 1;
	String curl = "results.jsp?query=" + queryString + "&maxresults=" + maxpage + "&startat=" + startindex+"&checkboxR="+checkR+"&label=1";
	for(int n = -7;n<9;n++){
		int curStartIndex = startindex + n * maxpage;
		int curPage = cpage + n;
		if(curStartIndex < 0 || curStartIndex >= hits.length()){
			continue;
		}
		String navUrl = "results.jsp?query=" + queryString + "&c="+searchObjectType+"&maxresults=" + maxpage + "&startat=" + curStartIndex+"&checkboxR="+checkR+"&label=1";
	%>
	<font face="鏂板畫浣�" size="2" color="red"><% if(n!=0){ %><a href="<%=navUrl%>"><b><%}%><%=curPage%><% if(n!=0){ %></b></a><%}%></font>
	<% } %>
	<%             
	if ( (startindex + maxpage) < hits.length()) {   //if there are more results...display the more link
		String moreurl="results.jsp?query=" + queryString +  "&c="+searchObjectType+"&maxresults=" + maxpage + 
				"&startat=" + (startindex + maxpage)+ "&checkboxR="+checkR+"&label=1";
	%>
	<font face="鏂板畫浣�" size="2"><a href="<%=moreurl%>">鍚庝竴椤�<img src="../images/right.gif" border='0' width="15"></a></font>
	<%
	}
	%>
	</div>
<%       }                                            //then include our footer.
%>
<% } //end if queryString != null %>
<%@include file="footer.jsp"%>        
</body>
</html>